import { getToken, removeToken } from '@/utils/auth'

// 登录页面
const loginPage = "/pages/login"
  
// 页面白名单
const whiteList = [
  '/pages/login',
  '/pages/common/webview/index'
]

// 权限白名单
const roleWhite = [
	'/pages/mine/avatar/index'
]

// 检查地址白名单
function checkWhite(url) {
  const path = url.split('?')[0]
  return whiteList.indexOf(path) !== -1
}

// 检查认证白名单
function checkAuthWhite(url) {
  const path = url.split('?')[0]
  return roleWhite.indexOf(path) !== -1
}

function interceptor(url){
	// return true
	console.log(url,"url=====")
	if (getToken()) {
		if (url === loginPage) {
			// uni.reLaunch({ url: "/pages/home/index" })
			return true
		}else{
			if (checkAuthWhite(url)) {
				return true
			}
		}
	} else {
		if (checkWhite(url)) {
			return true
		}
		uni.reLaunch({ url: loginPage })
		return false
	}
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
	uni.addInterceptor(item, {
		invoke(to) {
			return interceptor(to.url)
		},
		fail(err) {
			console.log(err)
		}
	})
})
	
export function navIntercept(url){
	interceptor(url)
}
